<?php

class Chungloai extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper(array('html','form', 'url', 'codegen_helper'));
        $this->load->model('codegen_model', '', TRUE);
       
    }

    function index() {
        $this->manage();
    }

    function manage() {
        $this->load->library('table');
        $this->load->library('pagination');

        //paging
        $config['base_url'] = base_url() . 'index.php/chungloai/manage/';
        $config['total_rows'] = $this->codegen_model->count('chungloai');
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        // make sure to put the primarykey first when selecting , 
        //eg. 'userID,name as Name , lastname as Last_Name' , Name and Last_Name will be use as table header.
        // Last_Name will be converted into Last Name using humanize() function, under inflector helper of the CI core.
        $this->data['results'] = $this->codegen_model->get('chungloai', '*', '', $config['per_page'], $this->uri->segment(3));

        $this->load->view('admin\chungloai\chungloai_list', $this->data);
        //$this->template->load('content', 'chungloai_list', $this->data); // if have template library , http://maestric.com/doc/php/codeigniter_template
    }

    function add() {
        $this->load->library('form_validation');
         $this->load->library('upload');
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('chungloai') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
        } else {
            if (isset($_FILES)) {
                $name = $_FILES['hinhdaidien']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_product/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinhdaidien')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
            $data = array(
                'ten_chung_loai' => set_value('ten_chung_loai'),
                'tieude__chungloai_sp' => set_value('tieude__chungloai_sp'),
                'gioithieu' => set_value('gioithieu'),
                'phukien' => set_value('phukien'),
                'thu_tu' => set_value('thu_tu'),
                'hinhdaidien' => $name,
                'AnHien' => set_value('AnHien')
            );

            if ($this->codegen_model->add('chungloai', $data) == TRUE) {
                //$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
                // or redirect
                redirect(base_url() . 'chungloai/manage/');
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';
            }
        }
        $this->load->view('admin\chungloai\chungloai_add', $this->data);
        //$this->template->load('content', 'chungloai_add', $this->data);
    }

    function edit() {
        $this->load->library('form_validation');
        $this->load->library('upload');
        $this->data['custom_error'] = '';

        if ($this->form_validation->run('chungloai') == false) {
            $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
        } else {
            $name = $_FILES['hinhdaidien']['name'];
            if (isset($_FILES)) {
               
                if (!empty($name)) {
                      
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_product/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinhdaidien')) {
                       
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
           
            if(!empty($name)){
                $data = array(
                    'ten_chung_loai' => $this->input->post('ten_chung_loai'),
                    'tieude__chungloai_sp' => $this->input->post('tieude__chungloai_sp'),
                    'gioithieu' => $this->input->post('gioithieu'),
                    'phukien' => $this->input->post('phukien'),
                    'thu_tu' => $this->input->post('thu_tu'),
                    'hinhdaidien' => $name,
                    'AnHien' => $this->input->post('AnHien')
                );
            }else{
                $data = array(
                    'ten_chung_loai' => $this->input->post('ten_chung_loai'),
                    'tieude__chungloai_sp' => $this->input->post('tieude__chungloai_sp'),
                    'gioithieu' => $this->input->post('gioithieu'),
                    'phukien' => $this->input->post('phukien'),
                    'thu_tu' => $this->input->post('thu_tu'),
                    'AnHien' => $this->input->post('AnHien')
                );
            }

            if ($this->codegen_model->edit('chungloai', $data, 'ma_chung_loai', $this->input->post('ma_chung_loai')) == TRUE) {
                redirect(base_url() . 'index.php/chungloai/manage/');
            } else {
                $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';
            }
        }

        $this->data['result'] = $this->codegen_model->get('chungloai', 'ma_chung_loai,ten_chung_loai,tieude__chungloai_sp,gioithieu,phukien,thu_tu,hinhdaidien,AnHien', 'ma_chung_loai = ' . $this->uri->segment(3), NULL, NULL, true);

        $this->load->view('admin\chungloai\chungloai_edit', $this->data);
        //$this->template->load('content', 'chungloai_edit', $this->data);
    }

    function delete() {
        $ID = $this->uri->segment(3);
        $this->codegen_model->delete('chungloai', 'ma_chung_loai', $ID);
        
        redirect(base_url() . 'index.php/chungloai/manage/');
    }

}

/* End of file chungloai.php */
/* Location: ./system/application/controllers/chungloai.php */